import { Button, ButtonProps } from "antd";

import { useGlobalContent } from "@/store";
import { UserInfo } from "@/store/index.d";

import "./index.less";

// 判断按钮是否有权限 true表示有 false没有
export const getButtonPower = (powerKey: string | string[], global: globalInitType): boolean => {
  const { userInfoData = {} as UserInfo, powerButtonList = [] as any[] } = global || {};
  // if (userInfoData?.sfcg) return true;

  if (typeof powerKey === "string") return powerButtonList.includes(powerKey);
  return powerKey.some((item) => powerButtonList.includes(item));
};

export interface IPowerButton extends ButtonProps {
  powerKey?: string | string[]; // 按钮权限key 对应菜单管理中按钮权限标识
}
/** 带权限判断的按钮 */
const PowerButton = (props: IPowerButton) => {
  const { disabled, powerKey, ...rest } = props;
  const { global } = useGlobalContent();

  const power = powerKey ? getButtonPower(powerKey, global) : true;

  return <Button disabled={!power || disabled} {...rest}></Button>;
};
export default PowerButton;
